//
//  WZWorkFlowListVC.m
//  WZEfengAndEtong
//
//  Created by wanzhao on 16/8/18.
//  Copyright © 2016年 wanzhao. All rights reserved.
//

#import "WZWorkFlowListVC.h"
#import "WZApplyListCell.h"
#import "WZWebViewController.h"

@interface WZWorkFlowListVC ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, weak) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *listArray;
@property (nonatomic, copy) NSString *applyUrl;

@end

@implementation WZWorkFlowListVC

static NSString *const identifier = @"applyListCell";

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupTableView];
    
    [self loadWorkFlowApplyList];
}

- (void)setupTableView {
    
    self.view.backgroundColor = WhiteColor;
    self.navigationItem.title = @"详细流程列表";
//    self.navigationItem.rightBarButtonItem = [UIBarButtonItem BarButtonItemWithImageName:@"shuaxin" title:nil target:self sel:@selector(shuaXinShuJu) titleFont:Font10 titleColor:WhiteColor];
    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, self.view.height)];
    self.tableView = tableView;
    [self.view addSubview:tableView];
    tableView.dataSource = self;
    tableView.delegate = self;
    
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([WZApplyListCell class]) bundle:nil] forCellReuseIdentifier:identifier];
}


- (void)loadWorkFlowApplyList {
    
    [MBProgressHUD showMessage:@"正在加载..." toView:self.view];
    [NetWorkShare getWorkFlowWithApplyAndLoginName:self.oaUserName password:self.oaPwd companyId:self.oaCompanyID FormId:self.formId successHandle:^(AFHTTPRequestOperation *operation, id responseObject) {
        if (responseObject) {
            
            WZLog(@"result = %@", responseObject);
            
            if ([[responseObject objectForKey:@"success"] boolValue]) {
                
                [MBProgressHUD hideHUDForView:self.view];
                [self.listArray removeAllObjects];
                self.listArray = [WZApplyList objectArrayWithKeyValuesArray:responseObject[@"datas"]];
                self.applyUrl = responseObject[@"applyUrl"];
                
                [self.tableView reloadData];
                
            } else {
                [MBProgressHUD hideHUDForView:self.view];
                [MBProgressHUD showError:[responseObject objectForKey:@"加载页面出错"] toView:self.view];
            }
        } else {
            
            [MBProgressHUD hideHUDForView:self.view];
            [MBProgressHUD showError:UNKNOWNERROR toView:self.view];
        }
    } failureHandle:^(AFHTTPRequestOperation *operation, NSError *error) {
        WZLog(@"error = %@", error);
        
        [MBProgressHUD hideHUDForView:self.view];
        [MBProgressHUD showError:NETWORKNERROR toView:self.view];
    }];
}

#pragma mark -- delegate 
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    WZApplyList *list = self.listArray[indexPath.row];
    
    if ([list.runState isEqualToString:@"运行中"]) {
        
        NSString *applyURLStr = [[NSString stringWithFormat:@"http:%@/OA_CBD%@&formid=%@&workFlowflag=%@&mversion=%@&taskdef=%@&fversion=%@",SERVERIP, self.applyUrl, list.formKey, list.modelId, list.mversion, list.startEventKey, list.fversion] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        
        NSString *autho = [[[NSString stringWithFormat:@"%@:%@:%@", self.oaUserName, self.oaPwd, self.oaCompanyID] dataUsingEncoding:NSUTF8StringEncoding] base64Encoding];
        NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:applyURLStr]];
        
        [req addValue:[NSString stringWithFormat:@"Basic %@", autho] forHTTPHeaderField:@"Authorization"];
        
        NSURLRequest *request = [req copy];
        WZWebViewController *web = [[WZWebViewController alloc] init];
        web.list = list;
        web.request = request;
        
        [self.navigationController pushViewController:web animated:YES];
    } else {
        
        [MBProgressHUD showError:@"流程非运行中!" toView:self.view];
    }
}

#pragma mark -- dataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.listArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    WZApplyListCell *cell=[tableView dequeueReusableCellWithIdentifier:identifier];
    cell.list = self.listArray[indexPath.row];
    return cell;
}

#pragma mark 懒加载
- (NSMutableArray *)listArray
{
    if (_listArray == nil) {
        _listArray = [NSMutableArray array];
    }
    return _listArray;
}


@end
